/*
 *    Copyright (c) 2014 Xiang Xu <xuxiang@mail.bnu.edu.cn>
 *
 *    Permission is hereby granted, free of charge, to any person
 *    obtaining a copy of this software and associated documentation
 *    files (the "Software"), to deal in the Software without
 *    restriction, including without limitation the rights to use,
 *    copy, modify, merge, publish, distribute, sublicense, and/or sell
 *    copies of the Software, and to permit persons to whom the
 *    Software is furnished to do so, subject to the following
 *    conditions:
 *
 *    The above copyright notice and this permission notice shall be
 *    included in all copies or substantial portions of the Software.
 *
 *    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 *    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 *    OTHER DEALINGS IN THE SOFTWARE.
 */

#ifndef RL_UTILITY_H
#define RL_UTILITY_H

#include "CNC/cnc_sparse_matrix.h"
#include <vector>

//! Utility functions for Reinforcement Learning.
class RLUtility
{
public:
    RLUtility() {}
    ~RLUtility() {}

    //! Get first max element in the array.
    /*!
     \ p*ar*am array the array.
     \param n the array size.
     \return the first max element in the array.
     \sa ArgMax(), MaxAll(), ArgMaxAll()
     */
    static double Max(double* array, int n);
    static double Max(const CNCSparseRowColumn<double>& row);

    //! Get first max element in the sub-array of an array.
    /*!
     \ p*ar*am array the array.
     \param index the sub-array's index in the array.
     \return the first max element in the sub-array.
     */
    static double Max(double* array, std::vector<int> index);

    //! Get first max element's index in the array.
    /*!
     \ p*ar*am array the array.
     \param n the array size.
     \return the first max element in the array.
     */
    static int ArgMax(double* array, int n);
    static int ArgMax(const CNCSparseRowColumn<double>& row);

    //! Get first max element's index in the sub-array of an array.
    /*!
     \ p*ar*am array the array.
     \param index the sub-array's index in the array.
     \return the first max element's index in the sub-array.
     */
    static int ArgMax(double* array, std::vector<int> index);

    //! Get all the max elements' indexes in the array.
    /*!
     \ p*ar*am array the array.
     \param n array size.
     \return all the max elements' indexes in the array.
     */
    static std::vector<int> ArgMaxAll(double* array, int n);
    static std::vector<int> ArgMaxAll(const CNCSparseRowColumn<double>& row);
    //! Get all the max elements' indexes in the sub-array of an array.
    /*!
     \ p*ar*am array the array.
     \param index the sub-array's index in the array.
     \return all the max elements' indexes in the sub-array of an array.
     */
    static std::vector<int> ArgMaxAll(double* array, std::vector<int> index);

    //! Generate an int in [0, A)
    static int Rand0A(int A);

    //! Generate an int in [A, B)
    static int RandAB(int A, int B);

    //! Generate a double in [0, 1]
    static double RandUnit();
};

#endif
